package tcpServer;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.SwingUtilities;

import util.User;

import db.DB;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ServerInterface extends javax.swing.JFrame implements ActionListener{
	private JTabbedPane tabPanel;
	private JPanel jPanel1;
	private JLabel disconnectUserJLabel;
	private JButton maxConnectionsJButton;
	private JLabel maxConnectionsAvailableJLabel;
	private JLabel maxUserConnectedJLabel;
	private JLabel unameModifyJLabel;
	private JLabel statusJLabel;
	private JButton deleteJButton;
	private JButton updateAddJButton;
	private JTextField addJTextField;
	private JLabel jLabel1;
	private JButton addJButton;
	private JButton updateModifyJButton;
	private JTextField passModifyJTextField;
	private JTextField unameModifyJTextField;
	private JLabel passModifyJLabel;
	private JButton modifyJButton;
	private JLabel usernameJLabel;
	private JTextField usernameJTextField;
	private JPanel jPanel4;
	private JPanel jPanel3;
	private JPanel Administration;
	private JLabel loggedUsersJLabel;
	private JTextArea loggedUsersJTextArea;
	private JLabel disconnectUser;
	private JButton disconnectUserJButton;
	private JTextField disconnectUserJTextField;
	private JTextField maxConnectionsJTextField;
	private JLabel maxConnectionsJLabel;
	private JLabel numberConnJLabel;
	private JLabel numConnectionsJLabel;
	private JPanel jPanel2;
	
	private DB dataBase = new DB();

	/**
	* Auto-generated main method to display this JFrame
	*/
		
	public ServerInterface() {
		super();
		initGUI();
		this.setVisible(true);
	}
	
	private void initGUI() {
		try {
			BorderLayout thisLayout = new BorderLayout();
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				tabPanel = new JTabbedPane();
				getContentPane().add(tabPanel, BorderLayout.CENTER);
				{
					jPanel1 = new JPanel();
					GridLayout jPanel1Layout = new GridLayout(1, 1);
					jPanel1Layout.setHgap(5);
					jPanel1Layout.setVgap(5);
					jPanel1Layout.setColumns(1);
					jPanel1.setLayout(jPanel1Layout);
					tabPanel.addTab("Connections", null, jPanel1, null);
					jPanel1.setPreferredSize(new java.awt.Dimension(459, 283));
					{
						jPanel2 = new JPanel();
						GridBagLayout jPanel2Layout = new GridBagLayout();
						jPanel2Layout.rowWeights = new double[] {0.1, 0.1, 0.1, 0.1};
						jPanel2Layout.rowHeights = new int[] {7, 7, 7, 7};
						jPanel2Layout.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.0, 30.0, 0.1, 0.1, 0.1, 0.1};
						jPanel2Layout.columnWidths = new int[] {20, 7, 20, 7, 20, 20, 50, 50, 20, 20, 7, 7};
						jPanel1.add(jPanel2);
						jPanel2.setLayout(jPanel2Layout);
						jPanel2.setPreferredSize(new java.awt.Dimension(310, 334));
						{
							numConnectionsJLabel = new JLabel();
							jPanel2.add(numConnectionsJLabel, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
							numConnectionsJLabel.setText("Number of connections:");
						}
						{
							numberConnJLabel = new JLabel();
							jPanel2.add(numberConnJLabel, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
							numberConnJLabel.setText("0");
						}
						{
							maxConnectionsJLabel = new JLabel();
							jPanel2.add(maxConnectionsJLabel, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
							maxConnectionsJLabel.setText("Maximun connections:");
						}
						{
							maxConnectionsJTextField = new JTextField();
							jPanel2.add(maxConnectionsJTextField, new GridBagConstraints(2, 1, 2, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
							maxConnectionsJTextField.setSize(40, 30);
							maxConnectionsJTextField.setPreferredSize(new java.awt.Dimension(40, 30));
						}
						{
							maxUserConnectedJLabel = new JLabel();
							jPanel2.add(maxUserConnectedJLabel, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
							maxUserConnectedJLabel.setText("Maximun connections available:");
						}
						{
							maxConnectionsAvailableJLabel = new JLabel();
							jPanel2.add(maxConnectionsAvailableJLabel, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
							maxConnectionsAvailableJLabel.setText("3");
						}
						{
							maxConnectionsJButton = new JButton();
							jPanel2.add(maxConnectionsJButton, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
							maxConnectionsJButton.setText("Update");
						}
						{
							disconnectUserJLabel = new JLabel();
							jPanel2.add(disconnectUserJLabel, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
							disconnectUserJLabel.setText("User ID:");
						}
						{
							disconnectUserJTextField = new JTextField();
							jPanel2.add(disconnectUserJTextField, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
							disconnectUserJTextField.setPreferredSize(new java.awt.Dimension(40, 30));
						}
						{
							disconnectUserJButton = new JButton();
							jPanel2.add(disconnectUserJButton, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
							disconnectUserJButton.setText("Disconnect");
						}
						{
							disconnectUser = new JLabel();
							jPanel2.add(disconnectUser, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
							disconnectUser.setText("Disconnect user");
						}
						{
							loggedUsersJTextArea = new JTextArea();
							jPanel2.add(loggedUsersJTextArea, new GridBagConstraints(7, 1, 1, 2, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
							loggedUsersJTextArea.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
						}
						{
							loggedUsersJLabel = new JLabel();
							jPanel2.add(loggedUsersJLabel, new GridBagConstraints(7, 0, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
							loggedUsersJLabel.setText("Logged users");
						}
					}
				}
				{
					Administration = new JPanel();
					GridLayout AdministrationLayout = new GridLayout(1, 1);
					AdministrationLayout.setHgap(5);
					AdministrationLayout.setVgap(5);
					AdministrationLayout.setColumns(1);
					Administration.setLayout(AdministrationLayout);
					tabPanel.addTab("Administration", null, Administration, null);
					{
						jPanel3 = new JPanel();
						Administration.add(jPanel3, "Connections");
						GridLayout jPanel3Layout = new GridLayout(1, 1);
						jPanel3Layout.setHgap(5);
						jPanel3Layout.setVgap(5);
						jPanel3Layout.setColumns(1);
						jPanel3.setPreferredSize(new java.awt.Dimension(459,283));
						jPanel3.setLayout(jPanel3Layout);
						{
							jPanel4 = new JPanel();
							GridBagLayout jPanel4Layout1 = new GridBagLayout();
							jPanel4Layout1.columnWidths = new int[] {20, 7, 7, 20, 20, 50, 20, 50, 20, 20, 7, 7};
							jPanel4Layout1.rowHeights = new int[] {7, 20, 7, 7, 7};
							jPanel4Layout1.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1, 0.1, 0.0, 0.1, 0.0, 0.1, 0.1, 0.1, 0.1};
							jPanel4Layout1.rowWeights = new double[] {0.1, 0.1, 0.1, 0.1, 0.1};
							jPanel3.add(jPanel4);
							jPanel4.setPreferredSize(new java.awt.Dimension(310,334));
							jPanel4.setLayout(jPanel4Layout1);
							{
								usernameJTextField = new JTextField();
								jPanel4.add(usernameJTextField, new GridBagConstraints(5, 1, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
								usernameJTextField.setSize(50, 30);
								usernameJTextField.setPreferredSize(new java.awt.Dimension(60, 30));
							}
							{
								usernameJLabel = new JLabel();
								jPanel4.add(usernameJLabel, new GridBagConstraints(5, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
								usernameJLabel.setText("Insert username:");
							}
							{
								modifyJButton = new JButton();
								jPanel4.add(modifyJButton, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
								modifyJButton.setText("Modify");
							}
							{
								unameModifyJLabel = new JLabel();
								jPanel4.add(unameModifyJLabel, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
								unameModifyJLabel.setText("User:");
							}
							{
								passModifyJLabel = new JLabel();
								jPanel4.add(passModifyJLabel, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
								passModifyJLabel.setText("Password:");
							}
							{
								unameModifyJTextField = new JTextField();
								jPanel4.add(unameModifyJTextField, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
							}
							{
								passModifyJTextField = new JTextField();
								jPanel4.add(passModifyJTextField, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
							}
							{
								updateModifyJButton = new JButton();
								jPanel4.add(updateModifyJButton, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
								updateModifyJButton.setText("Update");
							}
							{
								addJButton = new JButton();
								jPanel4.add(addJButton, new GridBagConstraints(5, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
								addJButton.setText("Add");
							}
							{
								jLabel1 = new JLabel();
								jPanel4.add(jLabel1, new GridBagConstraints(4, 3, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
								jLabel1.setText("Password:");
							}
							{
								addJTextField = new JTextField();
								jPanel4.add(addJTextField, new GridBagConstraints(5, 3, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
							}
							{
								updateAddJButton = new JButton();
								jPanel4.add(updateAddJButton, new GridBagConstraints(5, 3, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
								updateAddJButton.setText("Save");
							}
							{
								deleteJButton = new JButton();
								jPanel4.add(deleteJButton, new GridBagConstraints(9, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
								deleteJButton.setText("Delete");
							}
							{
								statusJLabel = new JLabel();
								jPanel4.add(statusJLabel, new GridBagConstraints(5, 4, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
								statusJLabel.setText("Updated");
							}
						}
					}
				}
			}
			this.setFocusCycleRoot(false);
			pack();
			setSize(600, 400);
			unameModifyJTextField.setEnabled(false);
			unameModifyJTextField.setPreferredSize(new java.awt.Dimension(50, 23));
			passModifyJTextField.setEnabled(false);
			passModifyJTextField.setPreferredSize(new java.awt.Dimension(50, 23));
			addJTextField.setEnabled(false);
			addJTextField.setPreferredSize(new java.awt.Dimension(50, 23));
			updateModifyJButton.setEnabled(false);
			updateAddJButton.setEnabled(false);
			statusJLabel.setVisible(false);
			modifyJButton.addActionListener(this);
			addJButton.addActionListener(this);
			deleteJButton.addActionListener(this);
			updateModifyJButton.addActionListener(this);
			updateAddJButton.addActionListener(this);
			
			maxConnectionsJButton.addActionListener(this);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
		
	}
	public void actionPerformed(ActionEvent ae) 
	{
		if(ae.getSource()==modifyJButton)
		{
			statusJLabel.setVisible(false);
			try
			{
				dataBase.connect();
				System.out.println(usernameJTextField.getText());
				User user = dataBase.getUser(usernameJTextField.getText());
				if(user==null)
				{
					System.out.println("user==null");
				}
				unameModifyJTextField.setEnabled(true);
				passModifyJTextField.setEnabled(true);
				updateModifyJButton.setEnabled(true);
				unameModifyJTextField.setText(user.getLogin());
				passModifyJTextField.setText(user.getPassword());
				dataBase.disconnect();
			}
			catch (ClassNotFoundException e) {e.printStackTrace();}
			catch (SQLException e) {e.printStackTrace();}
			
		}
		if(ae.getSource()==updateModifyJButton)
		{
			try
			{
				System.out.println("estoy dentro");
				dataBase.connect();
				dataBase.updateUserLogin(usernameJTextField.getText(), unameModifyJTextField.getText(), unameModifyJTextField.getText());
				dataBase.updateUserPass(unameModifyJTextField.getText(), passModifyJTextField.getText());
				dataBase.disconnect();
				statusJLabel.setText("UPDATED");
				statusJLabel.setVisible(true);
			} 
			catch (ClassNotFoundException e) {e.printStackTrace();}
			catch (SQLException e) {e.printStackTrace();}
		}
		if(ae.getSource()==addJButton)
		{
			statusJLabel.setVisible(false);
			addJTextField.setEnabled(true);
			updateAddJButton.setEnabled(true);
		}
		if(ae.getSource()==updateAddJButton)
		{
			try
			{
				dataBase.connect();
				User user = dataBase.getUser(usernameJTextField.getText());
				if(user==null && !(usernameJTextField.getText().equals("")))
				{
					dataBase.addUser(usernameJTextField.getText(), addJTextField.getText());
					statusJLabel.setText("ADDED");
					statusJLabel.setVisible(true);
				}
				else
				{
					statusJLabel.setText("User in DB");
				}
				dataBase.disconnect();
			}
			catch (ClassNotFoundException e) {e.printStackTrace();}
			catch (SQLException e) {e.printStackTrace();}
		}
		if(ae.getSource()==deleteJButton)
		{
			statusJLabel.setVisible(false);
			try
			{
				dataBase.connect();
				dataBase.removeUser(usernameJTextField.getText());
				dataBase.disconnect();
				statusJLabel.setText("DELETED");
				statusJLabel.setVisible(true);
			}
			catch (ClassNotFoundException e) {e.printStackTrace();}
			catch (SQLException e) {e.printStackTrace();}
		}
		if(ae.getSource()==maxConnectionsJButton)
		{
			String maxConn = maxConnectionsJTextField.getText();
			maxConnectionsAvailableJLabel.setText(maxConn);
		}
	}

	public void updateConnectionsLabel(int numConnections)
	{
		numberConnJLabel.setText(String.valueOf(numConnections));
	}
	public int getMaxConnections()
	{
		int maxConnections = 0;
		maxConnections = Integer.parseInt(maxConnectionsAvailableJLabel.getText());
		System.out.println(maxConnections + " + 1 = " + (maxConnections + 1));
		return maxConnections;
	}
	public void setLoggedUsers(String list)
	{
		loggedUsersJTextArea.setText(list);
	}
	public JButton getDisconnectButton()
	{
		return disconnectUserJButton;
	}
	public String getDisconnectUser()
	{
		System.out.println(disconnectUserJTextField.getText());
		return disconnectUserJTextField.getText();
	}
}
